//
//  RegAccountVC.m
//  genericframework
//
//  Created by 谷丰 on 2025/4/1.
//

#import "RegAccountVC.h"

<<<<<<< HEAD
#import "SettingMibaoVC.h"
@interface RegAccountVC ()<UITextFieldDelegate>
@property (nonatomic,strong)UIImageView * BgImageView;//登录背景
@property (nonatomic,strong)UIImageView * LogoImageView;//Logo
@property (nonatomic, strong) UITextField *accountTextField;   // 账号
@property (nonatomic, strong) UITextField *nicknameTextField;  // 昵称
@property (nonatomic, strong) UITextField *passwordTextField;  // 密码
@property (nonatomic, strong) UITextField *confirmPasswordTextField; // 确认密码
@property (nonatomic, strong) UITextField *inviteCodeTextField; // 邀请码

@property (nonatomic, strong) UIButton *loginButton;       // 注册按钮
@property (nonatomic, strong) UIButton *checkBox;          // 勾选框
@property (nonatomic, strong) UILabel *agreementLabel;     // 协议文本（包含可点击链接）
@property (nonatomic, assign) BOOL isAgreed;  // 是否同意协议
@property (nonatomic, strong) UIButton *GotoLogin;       // 去登录按钮
@end

@implementation RegAccountVC
-(UIImageView *)BgImageView{
    if (!_BgImageView) {
        _BgImageView = [[UIImageView alloc]init];
        _BgImageView.image = [UIImage imageNamed:@"背景"];
        _BgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.view addSubview:_BgImageView];
    }
    return _BgImageView;
}
-(UIImageView *)LogoImageView{
    if (!_LogoImageView) {
        _LogoImageView = [[UIImageView alloc]init];
        _LogoImageView.image = [UIImage imageNamed:@"logo"];
        _LogoImageView.contentMode = UIViewContentModeScaleAspectFill;
        _LogoImageView.layer.cornerRadius=FitSizeW(11);
        _LogoImageView.clipsToBounds=YES;
        [self.view addSubview:_LogoImageView];
    }
    return _LogoImageView;
}
// 注册按钮
- (UIButton *)loginButton {
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:GFString(@"注册") forState:UIControlStateNormal];
        _loginButton.titleLabel.font = GFBFont(16);
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginButton.backgroundColor = [BaseWay MainColor];
        
        // 设置圆角和边框
        _loginButton.layer.cornerRadius = FitSizeW(25);  // 高度30，圆角15
        _loginButton.layer.borderWidth = 1.0;
        _loginButton.layer.borderColor = [BaseWay MainColor].CGColor;
        
        // 添加点击事件
        [_loginButton addTarget:self action:@selector(RegButtonClicked) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}
// 去登录按钮
- (UIButton *)GotoLogin {
    if (!_GotoLogin) {
        _GotoLogin = [UIButton buttonWithType:UIButtonTypeCustom];
        [_GotoLogin setTitle:GFString(@"已有爱诺账号! 直接登录") forState:UIControlStateNormal];
        _GotoLogin.titleLabel.font = GFFont(14);
        [_GotoLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _GotoLogin.backgroundColor = [UIColor clearColor];
        // 添加点击事件
        [_GotoLogin addTarget:self action:@selector(BackRootVC) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_GotoLogin];
    }
    return _GotoLogin;
}
-(void)BackRootVC{
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.BgImageView.frame= CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self CompositionNavBarHidden:NO andHiddenBackButton:NO withTitle:GFString(@"注册")];
    [self.BaseBackButton setImage:[UIImage imageNamed:@"icon_返回 拷贝 2"] forState:0];
    self.BaseNavBarView.backgroundColor=[UIColor clearColor];
    self.LogoImageView.frame=CGRectMake(SCREEN_WIDTH/2-FitSizeW(30), NAVIGATION_BAR_HEIGHT+FitSizeW(53), FitSizeW(60), FitSizeW(60));
    // 添加所有 TextField
    [self.view addSubview:self.accountTextField];
    [self.view addSubview:self.nicknameTextField];
    [self.view addSubview:self.passwordTextField];
    [self.view addSubview:self.confirmPasswordTextField];
    [self.view addSubview:self.inviteCodeTextField];
    self.accountTextField.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.LogoImageView.frame)+FitSizeW(43), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    self.nicknameTextField.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.accountTextField.frame)+FitSizeW(10), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    self.passwordTextField.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.nicknameTextField.frame)+FitSizeW(10), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    self.confirmPasswordTextField.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.passwordTextField.frame)+FitSizeW(10), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    self.inviteCodeTextField.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.confirmPasswordTextField.frame)+FitSizeW(10), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    
    if (self.AccountText) {
        self.accountTextField.text  = self.AccountText;
    }
    
    self.loginButton.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.inviteCodeTextField.frame)+FitSizeW(85), SCREEN_WIDTH-FitSizeW(70), FitSizeW(50));
    self.GotoLogin.frame=CGRectMake(FitSizeW(35), CGRectGetMaxY(self.loginButton.frame)+FitSizeW(10), SCREEN_WIDTH-FitSizeW(70), FitSizeW(30));
    
    NSMutableAttributedString *LogoattributedString = [[NSMutableAttributedString alloc] initWithString:GFString(@"已有爱诺账号! 直接登录")];
    [LogoattributedString addAttribute:NSForegroundColorAttributeName
                             value:rgba(255, 255, 255, 1)
                             range:NSMakeRange(0, 7)]; // 前7个字白色
    [LogoattributedString addAttribute:NSForegroundColorAttributeName
                             value:[BaseWay MainColor]
                             range:NSMakeRange(8, 4)]; // 后4个字绿色
    [self.GotoLogin setAttributedTitle:LogoattributedString forState:UIControlStateNormal];
    self.isAgreed = NO;  // 默认未同意
        
    self.checkBox = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.checkBox setImage:[UIImage imageNamed:@"icon-未选择(1)"]  forState:UIControlStateNormal];
    [self.checkBox setImage:[UIImage imageNamed:@"icon-勾选 拷贝"] forState:UIControlStateSelected];
    [self.checkBox addTarget:self action:@selector(checkBoxTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.checkBox];
       // 使用 Auto Layout 布局
    self.checkBox.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.checkBox.topAnchor constraintEqualToAnchor:self.inviteCodeTextField.safeAreaLayoutGuide.bottomAnchor constant:FitSizeW(20)],
        [self.checkBox.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor constant:FitSizeW(37)],
        [self.checkBox.widthAnchor constraintEqualToConstant:FitSizeW(16)],
        [self.checkBox.heightAnchor constraintEqualToConstant:FitSizeW(16)]
    ]];
    self.agreementLabel = [[UILabel alloc] init];
    self.agreementLabel.userInteractionEnabled = YES;  // 允许交互
    self.agreementLabel.numberOfLines = 0;
       
       // 设置富文本
    NSString *fullText = GFString(@"我已阅读并同意《用户协议》和《隐私政策》");
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullText];
       
       // 设置整体文字颜色（黑色）
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, fullText.length)];
       
       // 设置《用户协议》为蓝色
    NSRange userAgreementRange = [fullText rangeOfString:GFString(@"《用户协议》")];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:userAgreementRange];
       
       // 设置《隐私政策》为蓝色
    NSRange privacyPolicyRange = [fullText rangeOfString:GFString(@"《隐私政策》")];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:privacyPolicyRange];
    [attributedString addAttribute:NSFontAttributeName value:GFFont(12) range:NSMakeRange(0, fullText.length)];
    self.agreementLabel.attributedText = attributedString;
    [self.view addSubview:self.agreementLabel];
       
       // 使用 Auto Layout 布局
    self.agreementLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [self.agreementLabel.centerYAnchor constraintEqualToAnchor:self.checkBox.centerYAnchor],
        [self.agreementLabel.leadingAnchor constraintEqualToAnchor:self.checkBox.trailingAnchor constant:FitSizeW(10)],
        [self.agreementLabel.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor constant:-FitSizeW(20)]
    ]];
       
       // 添加点击手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleAgreementLabelTap:)];
    [self.agreementLabel addGestureRecognizer:tapGesture];
    // 使用 Auto Layout 布局
    
    // Do any additional setup after loading the view.
}
#pragma mark - 勾选框点击事件
- (void)checkBoxTapped {
    self.isAgreed = !self.isAgreed;
    self.checkBox.selected = self.isAgreed;
    NSLog(@"用户协议状态：%@", self.isAgreed ? @"已同意" : @"未同意");
}

#pragma mark - 协议文本点击事件
- (void)handleAgreementLabelTap:(UITapGestureRecognizer *)gesture {
    UILabel *label = (UILabel *)gesture.view;
    NSString *text = label.text;
    
    // 获取点击位置
    CGPoint tapLocation = [gesture locationInView:label];
    
    // 计算《用户协议》和《隐私政策》的文本范围
    NSRange userAgreementRange = [text rangeOfString:GFString(@"《用户协议》")];
    NSRange privacyPolicyRange = [text rangeOfString:GFString(@"《隐私政策》")];
    
    // 检测是否点击了《用户协议》
    if ([self isTapPoint:tapLocation inRange:userAgreementRange forLabel:label]) {
        NSLog(@"点击了《用户协议》");
        [self showUserAgreement];
        return;
    }
    
    // 检测是否点击了《隐私政策》
    if ([self isTapPoint:tapLocation inRange:privacyPolicyRange forLabel:label]) {
        NSLog(@"点击了《隐私政策》");
        [self showPrivacyPolicy];
        return;
    }
    
    // 如果点击其他部分，切换勾选状态
    [self checkBoxTapped];
}

// 判断点击位置是否在指定文本范围内
- (BOOL)isTapPoint:(CGPoint)point inRange:(NSRange)targetRange forLabel:(UILabel *)label {
    // 创建文本容器
    NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:label.bounds.size];
    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
    NSTextStorage *textStorage = [[NSTextStorage alloc] initWithAttributedString:label.attributedText];
    
    [textStorage addLayoutManager:layoutManager];
    [layoutManager addTextContainer:textContainer];
    
    // 计算目标文本的 rect
    NSRange glyphRange = [layoutManager glyphRangeForCharacterRange:targetRange actualCharacterRange:NULL];
    CGRect boundingRect = [layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer];
    
    // 检查点击位置是否在范围内
    return CGRectContainsPoint(boundingRect, point);
}

#pragma mark - 跳转到协议页面
- (void)showUserAgreement {
    // 跳转用户协议页面（示例）
    NSLog(@"跳转到用户协议页面");
    
   
}

- (void)showPrivacyPolicy {
    // 跳转隐私政策页面（示例）
    NSLog(@"跳转到隐私政策页面");
}
// 账号输入框（8-16位数字或字母）
- (UITextField *)accountTextField {
    if (!_accountTextField) {
        _accountTextField = [self createTextFieldWithPlaceholder:GFString(@"设置账号（8-16位数字或字母）")  leftImageName:@"icon-登录账号"];
        _accountTextField.keyboardType = UIKeyboardTypeASCIICapable;
        _accountTextField.clearsOnBeginEditing = YES;
        _accountTextField.clearButtonMode=  UITextFieldViewModeAlways;
        _accountTextField.delegate = self;
    }
    return _accountTextField;
}

// 昵称输入框
- (UITextField *)nicknameTextField {
    if (!_nicknameTextField) {
        _nicknameTextField = [self createTextFieldWithPlaceholder:GFString(@"设置昵称")  leftImageName:@"icon-昵称"];
        _nicknameTextField.clearsOnBeginEditing = YES;
        _nicknameTextField.clearButtonMode=  UITextFieldViewModeAlways;
    }
    return _nicknameTextField;
}

// 密码输入框
- (UITextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = [self createTextFieldWithPlaceholder:GFString(@"设置密码")  leftImageName:@"icon-密码"];
        _passwordTextField.secureTextEntry = YES; // 密码模式
        _passwordTextField.clearsOnBeginEditing = YES;
        _passwordTextField.clearButtonMode=  UITextFieldViewModeAlways;
    }
    return _passwordTextField;
}

// 确认密码输入框
- (UITextField *)confirmPasswordTextField {
    if (!_confirmPasswordTextField) {
        _confirmPasswordTextField = [self createTextFieldWithPlaceholder:GFString(@"再次确认密码")  leftImageName:@"icon-密码"];
        _confirmPasswordTextField.secureTextEntry = YES;
        _confirmPasswordTextField.clearsOnBeginEditing = YES;
        _confirmPasswordTextField.clearButtonMode=  UITextFieldViewModeAlways;
    }
    return _confirmPasswordTextField;
}

// 邀请码输入框
- (UITextField *)inviteCodeTextField {
    if (!_inviteCodeTextField) {
        _inviteCodeTextField = [self createTextFieldWithPlaceholder:GFString(@"请输入邀请码")  leftImageName:@"icon-邀请码"];
        _inviteCodeTextField.clearsOnBeginEditing = YES;
        _inviteCodeTextField.clearButtonMode=  UITextFieldViewModeAlways;
    }
    return _inviteCodeTextField;
}

#pragma mark - 创建通用 TextField（带 leftView 和底部白线）
- (UITextField *)createTextFieldWithPlaceholder:(NSString *)placeholder leftImageName:(NSString *)imageName {
    UITextField *textField = [[UITextField alloc] init];
    textField.placeholder = placeholder;
    textField.font = GFFont(14);
    textField.textColor = [UIColor whiteColor];
    
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: rgba(255, 255, 255, 1),NSFontAttributeName: GFFont(14)}];
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 25, FitSizeW(50))];
    leftView.backgroundColor=[UIColor clearColor];
    // 设置左侧图标（20×20）
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
    leftImageView.frame = CGRectMake(0, FitSizeW(17.5), FitSizeW(15), FitSizeW(15));
    leftImageView.contentMode = UIViewContentModeScaleAspectFit;
    [leftView addSubview:leftImageView];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    // 底部白色线条
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor whiteColor];
    [textField addSubview:bottomLine];
    
    // 使用 Auto Layout 布局底部线条
    bottomLine.translatesAutoresizingMaskIntoConstraints = NO;
    [NSLayoutConstraint activateConstraints:@[
        [bottomLine.leadingAnchor constraintEqualToAnchor:textField.leadingAnchor],
        [bottomLine.trailingAnchor constraintEqualToAnchor:textField.trailingAnchor],
        [bottomLine.bottomAnchor constraintEqualToAnchor:textField.bottomAnchor],
        [bottomLine.heightAnchor constraintEqualToConstant:1]
    ]];
    
    return textField;
}




#pragma mark - UITextFieldDelegate（限制账号输入 8-16 位数字或字母）
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField == self.accountTextField) {
        // 只允许输入字母和数字
        NSCharacterSet *allowedChars = [NSCharacterSet alphanumericCharacterSet];
        NSCharacterSet *inputChars = [NSCharacterSet characterSetWithCharactersInString:string];
        if (![allowedChars isSupersetOfSet:inputChars]) {
            return NO;
        }
        
        // 限制长度 8-16 位
        NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
        return newText.length <= 16;
    }
    return YES;
}
//注册点击
-(void)RegButtonClicked{
    
    if (self.accountTextField.text.length<1||self.accountTextField.text.length>16) {
        [self ShowTextString:GFString(@"账号格式不正确") andWithTime:1 dissmess:^(id  _Nonnull responseObject) {
                    
        }];
    }else if (self.nicknameTextField.text.length<1) {
        [self ShowTextString:GFString(@"请输入昵称") andWithTime:1 dissmess:^(id  _Nonnull responseObject) {
                    
        }];
    }else if (self.passwordTextField.text.length<1) {
        [self ShowTextString:GFString(@"请输入密码") andWithTime:1 dissmess:^(id  _Nonnull responseObject) {
                    
        }];
    }else if (self.confirmPasswordTextField.text.length<1||(![self.passwordTextField.text isEqualToString:self.confirmPasswordTextField.text])) {
        [self ShowTextString:GFString(@"请再次确认密码") andWithTime:1 dissmess:^(id  _Nonnull responseObject) {
                    
        }];
    }else if (!self.isAgreed) {
        [self ShowTextString:GFString(@"请阅读隐私协议") andWithTime:1 dissmess:^(id  _Nonnull responseObject) {
                    
        }];
    }else {
        
//        NSMutableDictionary  * parameters =[[NSMutableDictionary alloc]init];
        SettingMibaoVC * setting = [[SettingMibaoVC alloc]init];
        
        
        
        
    }
    
    
    
    
    
    
    
    
    
}
=======
@interface RegAccountVC ()

@end

@implementation RegAccountVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

>>>>>>> 291cab90cbb8ff4921e32ec95b13330bf79c214c
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
